package com.wx.spider.common.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author:feng
 * @create:2021-04-26 12:04
 */
public class JsoupUtil {
    public static Document getDocument(String url){
        try {
            return Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Map<String, String> parseWxLink(String link){
        try {
            Document document = getDocument(link);

            if(document == null){
                return null;
            }

            HashMap<String, String> map = new HashMap<>();

            //标题
            Element activityName = document.getElementById("activity-name");
            if (activityName != null){
                String title = activityName.ownText();
                map.put("title", title);
            }

            Element metaContent = document.getElementById("meta_content");
            if(metaContent != null){
                //媒体
                Element jsName = metaContent.getElementById("js_name");
                if(jsName != null){
                    map.put("media",jsName.ownText());
                }
                //作者
                Elements authorEles = metaContent.getElementsByClass("rich_media_meta rich_media_meta_text");
                if(authorEles != null && authorEles.size() > 0){
                    String author = authorEles.first().ownText();
                    map.put("author", author);
                }
            }

            //时间
            String html = document.html();
            int wxpt = html.indexOf("e(t,n,i,document.getElementById(\"publish_time\"));");
            if (wxpt != -1) {
                String pubtime = html.substring(wxpt-13,wxpt - 3) + " 00:00:00";
                map.put("pubtime", pubtime);
            }

            //正文
            Element jsContent = document.getElementById("js_content");
            if (jsContent != null){
                String contentHtml = jsContent.html();
                map.put("content", contentHtml);
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Map<String, String> getWxContentAndMd(String link){
        try {
            Document document = getDocument(link);

            if(document == null){
                return null;
            }

            HashMap<String, String> map = new HashMap<>();


            Element metaContent = document.getElementById("meta_content");
            if(metaContent != null){
                //媒体
                Element jsName = metaContent.getElementById("js_name");
                if(jsName != null){
                    map.put("media",jsName.ownText());
                }
            }


            //正文
            Element jsContent = document.getElementById("js_content");
            if (jsContent != null){
                String contentHtml = jsContent.html();
                map.put("content", contentHtml);
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
//        Map<String, String> map = getWxContentAndMdImg("https://mp.weixin.qq.com/s/Vd8Fo54yB4uh3TV5mGxZkA");
        Map<String, String> map = parseWxLink("http://mp.weixin.qq.com/s?__biz=MzA4MTk5MDUzNA==&mid=2651677104&idx=1&sn=f18279af4b824389df4a55bdf19237fc");
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for(Map.Entry<String, String> entry : entrySet){
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        String content = HtmlUtil.getTextFromHtml(map.get("content"));
        System.out.println("正文：\r\n" + content);

    }

    public static Map<String, String> getWxContentAndMdImg(String link) {
        try {
            Document document = getDocument(link);

            if(document == null){
                return null;
            }

            HashMap<String, String> map = new HashMap<>();


            Elements mediaEles = document.getElementsByClass("js_go_profile account_nickname");
            if(mediaEles != null && mediaEles.size() > 0){
                Elements eles = mediaEles.first().getElementsByClass("account_nickname_inner");
                if(eles != null && eles.size() > 0){
                    String media = eles.first().ownText();
                    map.put("media", media);
                }
            }


            //正文
            Element jsContent = document.getElementById("js_content");
            if (jsContent != null){
                String contentHtml = jsContent.html();
                map.put("content", contentHtml);
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getWxContent(String link) {
        try {
            Document document = getDocument(link);
            //正文
            Element jsContent = document.getElementById("js_content");
            if (jsContent != null){
                String contentHtml = jsContent.html();
               return contentHtml;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }
}
